<template>
    <section>
        <!--工具条/搜索框-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <!--inline:行内表单样式  model:表单数据对象-->
            <el-form :inline="true" :model="filters">

                <el-form-item>
                    <el-input v-model="filters.name" placeholder="名称"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="getEmperor">查询</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="handleAdd" style="width: 100px">新增</el-button>
                </el-form-item>

            </el-form>
        </el-col>


        <!--列表-->
        <el-table :data="emperor" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60"></el-table-column>

            <el-table-column prop="name" label="客户姓名"  sortable></el-table-column>

            <el-table-column prop="phone" label="手机号码"  sortable></el-table-column>

            <el-table-column prop="email" label="邮箱"  sortable></el-table-column>


            <el-table-column label="操作" width="250">
                <template scope="scope">

                    <!--维修单的按钮-->
                    <el-popover
                            placement="right"
                            width="800"
                            trigger="click">
                        <!--工具条/搜索框-->
                        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                            <!--inline:行内表单样式  model:表单数据对象-->
                            <el-form :inline="true" :model="filters">
                                <el-form-item>
                                    <el-input v-model="filters.name" placeholder="名称"></el-input>
                                </el-form-item>

                                <el-form-item>
                                    <el-button type="primary" v-on:click="">查询</el-button>
                                </el-form-item>

                                <el-form-item>
                                    <el-button type="primary" @click="addMaintainss(scope.$index, scope.row)" style="width: 100px">新增</el-button>
                                </el-form-item>

                            </el-form>
                        </el-col>
                        <el-table :data="gridData" stripe>
                            <el-table-column type="index" width="60"></el-table-column>
                            <el-table-column  property="description" label="故障说明"></el-table-column>
                            <el-table-column  property="repairname" label="维修员"></el-table-column>
                            <el-table-column  property="after_seller_name" label="结算员"></el-table-column>
                            <el-table-column  property="send_car" label="是否还车"></el-table-column>
                            <el-table-column  property="is_finish" label="维修进度"></el-table-column>
                            <el-table-column label="操作" width="200">
                                <template scope="scope">
                                    <el-button size="small" @click="handleMaintain(scope.$index, scope.row)">编辑</el-button>
                                    <el-button type="danger" size="small" @click="handleDelMaintain(scope.$index, scope.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-button slot="reference" @click="clickMaintain(scope.$index, scope.row)">维修单</el-button>
                    </el-popover>

                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-size="10"
                    layout="total, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-col>


        <!--新增维修单的时候弹出编辑界面-->
        <el-dialog title="修改维修单" :visible.sync="addMaintainFormVisible" :close-on-click-modal="false" style="height: 800px;width: 1500px;">
            <el-form :model="addMaintainForm" label-width="80px" :rules="addMaintainFormRules" ref="addMaintainForm">

                <el-form-item label="维修员" prop="role_id">
                    <el-select v-model="addMaintainForm.repair_id" placeholder="请选择维修员">
                        <el-option
                                v-for="item in list_repair"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="售后员" prop="role_id">
                    <el-select v-model="addMaintainForm.after_seller_id" placeholder="请选择售后员">
                        <el-option
                                v-for="item in list_after_seller"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="送车服务" prop="qw">
                    <el-switch on-text="1" off-text="0" v-model="addMaintainForm.send_car"></el-switch>
                </el-form-item>

                <el-form-item label="故障说明" prop="desc">
                    <el-input type="textarea" v-model="addMaintainForm.description"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addMaintainFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addMaintainSubmit" :loading="addMaintain">提交</el-button>
            </div>
        </el-dialog>

        <!--修改维修单的时候弹出编辑界面-->
        <el-dialog title="修改维修单" :visible.sync="editMaintainFormVisible" :close-on-click-modal="false" style="height: 800px;width: 1500px;">
            <el-form :model="editMaintainForm" label-width="80px" :rules="editMaintainFormRules" ref="editMaintainForm">

                <el-form-item label="维修员" prop="role_id">
                    <el-select v-model="editMaintainForm.repair_id" placeholder="请选择维修员">
                        <el-option
                                v-for="item in list_repair"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="售后员" prop="role_id">
                    <el-select v-model="editMaintainForm.after_seller_id" placeholder="请选择售后员">
                        <el-option
                                v-for="item in list_after_seller"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="送车服务">
                    <el-switch on-text="1" off-text="0" v-model="editMaintainForm.send_car"></el-switch>
                </el-form-item>

                <el-form-item label="故障说明" prop="desc">
                    <el-input type="textarea" v-model="editMaintainForm.description"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editMaintainFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editMaintainSubmit" :loading="addMaintain">提交</el-button>
            </div>
        </el-dialog>



        <!--修改的时候 弹出 编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="客户姓名" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="editForm.phone" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">

            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">

                <el-form-item label="客户姓名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email" auto-complete="off"></el-input>
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">创建</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    export default {
        data() {
            return {
                filters:{
                    name:'', //搜索框默认为空
                },
                currentPage:1,//默认第一页
                total:0,// 默认0条数据
                sels: [],//列表选中列
                listLoading:false,//如果等于false 现在加载中效果
                editLoading:false,//修改加载中
                addLoading:false,//新增加载
                addMaintain:false,//新增维修员加载
                emperor:[], //所有用户的信息
                list_repair:[],//所有维修员的信息
                list_after_seller:[],//所有所有售后员
                editFormVisible:false,//默认不显示对话框
                addFormVisible:false,//新增对话框 是否显示 false 不显示新增对话框
                addMaintainFormVisible:false,//新增维修单对话框 是否显示 false 不显示新增对话框
                editMaintainFormVisible:false,//修改维修单对话框 是否显示 false 不显示新增对话框
                ids:0,
                editForm:{
                    id:0,
                    name:'',
                    phone:'',
                    email:'',
                    role_id:'',
                    qw:'',
                    description:'',
                }, //修改form对象
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }//required 必填的意思
                    ],
                    phone: [
                            { required: true, message: '请输入手机号码', trigger: 'blur' }//required 必填的意思
                    ],
                    email: [
                        { required: true, message: '请输入电子邮箱', trigger: 'blur' }//required 必填的意思
                    ]
                },

                addForm:{
                    name:'',
                    phone:'',
                    email:'',
                },
                //新增form对象
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入手机号码', trigger: 'blur' }//required 必填的意思
                    ],
                    email: [
                        { required: true, message: '请输入电子邮箱', trigger: 'blur' }//required 必填的意思
                    ]
                },

                addMaintainForm:{
                    customer_id:0,
                    repair_id:"",
                    after_seller_id:"",
                    description:"",
                    is_finish:0,
                    send_car:false,
                },//新增维修单form对象
                addMaintainFormRules: {

                },

                //修改维修单
                editMaintainForm:{
                    id:0,
                    customer_id:0,
                    repair_id:"",
                    after_seller_id:"",
                    description:"",
                    is_finish:0,
                    send_car:true,
                },//修改维修单form对象
                editMaintainFormRules: {

                },

                gridData: [{
                    description: '',
                    repairname: '',
                    after_seller_name: ''
                }]
            }
        },
        methods: {
            //分页工具条方法
            handleCurrentChange(val){
                //当前页
                this.currentPage = val;
                this.getEmperor();
            },
            //前往指定页
            handleCurrentChange(val){
                this.currentPage = val;
                this.getEmperor();
            },
            //查询所有的客户
            getEmperor(){
                //发送axios请求到后端 查询客户
                // http://localhost:80/emperor/list
                let param = {
                    name:this.filters.name,
                    currentPage: this.currentPage,
                };
                this.$http.patch('/customer/queryPage',param).then(res =>{
                    this.total = res.data.total;
                    this.emperor = res.data.rows;
                })
            },


            //新增弹出对话框
            handleAdd(){
                //清空表单！
                this.addForm.name="";
                this.addForm.phone="";
                this.addForm.email="";
                //弹出表单
                this.addFormVisible = true;
            },


            //编辑弹出对话框
            handleEdit(index,row){
                //打开修改的对话框
                this.editFormVisible = true;
                //回显
                this.editForm = row;
            },
            //编辑维修单弹出对话框
            handleMaintain(index,row){
                //显示维修员
                this.$http.patch('/order/list_repair').then(res =>{
                    this.list_repair = res.data;
                    for(var i = 0;i < res.data.length;i++){
                        if(res.data[i].username == row.repairname){
                            this.editMaintainForm.repair_id = res.data[i].id;
                        }
                    }
                });

                //显示接待员
                this.$http.patch('/order/list_after_seller').then(res =>{
                    this.list_after_seller = res.data;
                    for(var i = 0;i < res.data.length;i++){
                        if(res.data[i].username == row.after_seller_name){
                            this.editMaintainForm.after_seller_id = res.data[i].id;
                        }
                    }
                });

                if(row.send_car == "否"){
                    this.editMaintainForm.send_car = false;
                } else {
                    this.editMaintainForm.send_car = true;
                }

                //打开修改的对话框
                this.editMaintainFormVisible = true;
                //回显
                this.editMaintainForm = row;
                console.debug(row.send_car);

            },

            //新增维修单弹出对话框
            addMaintainss(index,row){
                //显示维修员
                this.$http.patch('/order/list_repair').then(res =>{
                    this.list_repair = res.data;
                });

                //显示接待员
                this.$http.patch('/order/list_after_seller').then(res =>{
                    this.list_after_seller = res.data;
                });

                this.addMaintainForm.repair_id = "",
                this.addMaintainForm.after_seller_id="",
                this.addMaintainForm.description="",
                this.addMaintainForm.send_car=false,

                this.addMaintainForm.customer_id = row.id;

                //打开修改的对话框
                this.addMaintainFormVisible = true;

            },

            //点击维修单的时候
            clickMaintain(index,row){
                this.ids = row.id;

                //显示这个用户的所有维修单
                this.$http.patch('/order/kh/'+row.id).then(res =>{

                    for(var i = 0;i < res.data.length;i++){
                        if(res.data[i].send_car){
                            res.data[i].send_car = "是";
                        } else {
                            res.data[i].send_car = "否";
                        }
                    }

                    for(var i = 0;i < res.data.length;i++){
                        if(res.data[i].is_finish == 0){
                            res.data[i].is_finish = "未开始";
                        } else if (res.data[i].is_finish == 1){
                            res.data[i].is_finish = "正在维修";
                        } else if (res.data[i].is_finish == 2){
                            res.data[i].is_finish = "维修完毕";
                        }
                    }

                    this.gridData = res.data;
                });
            },

            getclickMaintain(){
                //显示这个用户的所有维修单
                this.$http.patch('/order/kh/'+this.ids).then(res =>{

                    for(var i = 0;i < res.data.length;i++){
                        if(res.data[i].send_car){
                            res.data[i].send_car = "是";
                        } else {
                            res.data[i].send_car = "否";
                        }
                    }

                    for(var i = 0;i < res.data.length;i++){
                        if(res.data[i].is_finish == 0){
                            res.data[i].is_finish = "未开始";
                        } else if (res.data[i].is_finish == 1){
                            res.data[i].is_finish = "正在维修";
                        } else if (res.data[i].is_finish == 2){
                            res.data[i].is_finish = "维修完毕";
                        }
                    }

                    this.gridData = res.data;
                });
            },

            //删除方法
            handleDel(index,row){
                let id = row.id;
                this.$http.delete('/customer/'+id).then(res =>{
                    // {isSuccess:true,msg:'xxx'}
                    console.log(res.data);
                    let {success,message} = res.data;
                    if (success){
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                        this.getEmperor();
                    }else{
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    }
                })
            },
            //新增保存操作
            //新增
            addSubmit: function () {
                //提交之前的验证工作--elementui验证
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.addForm);
                            this.$http.put('/customer/save',para).then((res) => {
                                //去掉转圈圈
                                this.addLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['addForm'].resetFields();
                                    //关闭对话框
                                    this.addFormVisible = false;
                                    this.getEmperor();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增保存
            editSubmit(){
                //和新增类似
                //提交之前的验证工作--elementui验证
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addMaintain = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('/customer/update',para).then((res) => {
                                //去掉转圈圈
                                this.addMaintain = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['editForm'].resetFields();
                                    //关闭对话框
                                    this.editFormVisible = false;
                                    this.getEmperor();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });

            },
            //新增维修单保存
            addMaintainSubmit(){
                //和新增类似
                //提交之前的验证工作--elementui验证
                this.$refs.addMaintainForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.editLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.addMaintainForm);
                            this.$http.put('/order/save',para).then((res) => {
                                //去掉转圈圈
                                this.editLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['addMaintainForm'].resetFields();
                                    //关闭对话框
                                    this.addMaintainFormVisible = false;
                                    this.getEmperor();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //修改维修单保存
            editMaintainSubmit(){
                //和新增类似
                //提交之前的验证工作--elementui验证
                this.$refs.editMaintainForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {

                            if(this.editMaintainForm.is_finish == "未开始"){
                                this.editMaintainForm.is_finish = 0;
                            } else if (this.editMaintainForm.is_finish == "正在维修"){
                                this.editMaintainForm.is_finish = 1;
                            } else if (this.editMaintainForm.is_finish == "维修完毕"){
                                this.editMaintainForm.is_finish = 2;
                            }

                            if(this.editMaintainForm.send_car == "是"){
                                this.editMaintainForm.send_car = true;
                            } else {
                                this.editMaintainForm.send_car = false;
                            }
                            //出现加载中效果
                            this.editLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.editMaintainForm);
                            console.debug(para);

                            this.$http.post('/order/update',para).then((res) => {
                                //去掉转圈圈
                                this.editLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['editMaintainForm'].resetFields();
                                    //关闭对话框
                                    this.editMaintainFormVisible = false;
                                    this.getEmperor();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //删除方法
            handleDelMaintain(index,row){
                let id = row.id;
                this.$http.delete('/order/'+id).then(res =>{
                    // {isSuccess:true,msg:'xxx'}
                    let {success,message} = res.data;
                    if (success){
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                        this.getclickMaintain();
                    }else{
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    }
                })
            },

            //多选方法
            selsChange: function (sels) {
                this.sels = sels;
            },
        },
        mounted() {
            //挂载结束 el:#myDiv 所有vue内容 都渲染到DOM--虚拟DOM
            //发送请求
            this.getEmperor()
        }
    }
</script>

<style scoped>

</style>